package com.dreamer.view.sub;

import com.dreamer.domain.pmall.goods.PmallGoods;
import com.dreamer.domain.pmall.goods.PmallGoodsType;
import com.dreamer.domain.sub.SubGoods;
import com.dreamer.domain.user.AgentLevel;
import com.dreamer.domain.user.User;
import com.dreamer.service.mobile.AgentLevelHandler;
import com.dreamer.service.mobile.MallGoodsHandler;
import com.dreamer.service.mobile.MallGoodsTypeHandler;
import com.dreamer.service.mobile.SubGoodsHandler;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;
import ps.mx.otter.utils.SearchParameter;
import ps.mx.otter.utils.WebUtil;

import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.List;
import java.util.Optional;

@Controller
@RequestMapping("/sub/goods")
public class SubGoodsQueryController {

	@RequestMapping(value = { "/index.html", "/search.html" }, method = RequestMethod.GET)
	public String index(
			@ModelAttribute("parameter") SearchParameter<PmallGoods> parameter, HttpServletRequest request, Model model) {
		try {
			User user = (User) WebUtil.getCurrentUser(request);
			model.addAttribute("users",user);
			/*List<PmallGoods> goods = mallGoodsDAO.searchEntityByPage(parameter, null,(t) -> true);
			WebUtil.turnPage(parameter, request);
			model.addAttribute("goods", goods);*/
//			List<PmallGoodsType> pmallGoodsTypes = mallGoodsTypeHandler.findSubType();
//			model.addAttribute("types", pmallGoodsTypes);
//			PmallGoods p = mallGoodsHandler.get(parameter.getEntity().getId());
//			p.setSubGoods("置换商城");
//			parameter.setEntity(p);
			List<PmallGoods> goods = new ArrayList<>();
			if(user.getId() != 1 && !user.getRealName().equals("管理员")){
				//todo 处理不是管理员的的商品，只展示当前登录人的商品.
				goods = subGoodsHandler.findSubGoodsByUserId(user.getId());
			}else{
				//todo 所有

				goods = mallGoodsHandler.findMallGoods_sub(parameter);
			}
			WebUtil.turnPage(parameter, request);
			model.addAttribute("subGoods",goods);
		} catch (Exception exp) {
			exp.printStackTrace();
			LOG.error("置换商城产品查询失败", exp);
		}
		return "/sub/subGoods_index";
	}
	
//	@RequestMapping(value = { "/query.json", "/search.json" }, method = RequestMethod.GET)
//	@ResponseBody
//	public DatatableDTO<SubGoods> queryASJson(
//			@ModelAttribute("parameter") SearchParameter<SubGoods> parameter,
//			HttpServletRequest request) {
//		DatatableDTO<SubGoods> dts=new DatatableDTO<>();
//		try {
//            List<SubGoods> goods = subGoodsHandler.findMallGoods(parameter);
//            WebUtil.turnPage(parameter, request);
//			dts.setData(goods);
//			dts.setRecordsFiltered(parameter.getTotalRows());
//			dts.setRecordsTotal(parameter.getTotalRows());
//		} catch (Exception exp) {
//			exp.printStackTrace();
//			LOG.error("置换商城产品查询失败", exp);
//		}
//		return dts;
//	}

	@RequestMapping(value = "/edit.html", method = RequestMethod.GET)
	public String edit_enter(
			@ModelAttribute("parameter") SearchParameter<PmallGoods> parameter,
			HttpServletRequest request, Model model) {
		User user = (User) WebUtil.getCurrentUser(request);
		//if (user.isAdmin()) {
//			model.addAttribute("types", PmallGoodsType.values());
            List<PmallGoodsType> pmallGoodsTypes = mallGoodsTypeHandler.findSubType();
            model.addAttribute("types", pmallGoodsTypes);
            List<AgentLevel> levels = agentLevelHandler.findAllOrderByLevel();
            model.addAttribute("levels",levels);
			return "/sub/subGoods_edit";
		//} else {
			//LOG.error("非管理员身份,无置换商城商品编辑权限");
			//return "/common/403";
		//}

	}

	@RequestMapping(value = "/edit1.html", method = RequestMethod.GET)
	public String edit_enter1(
			@ModelAttribute("parameter") SearchParameter<PmallGoods> parameter,
			HttpServletRequest request, Model model) {
		List<PmallGoodsType> pmallGoodsTypes = mallGoodsTypeHandler.findSubType();
		model.addAttribute("types", pmallGoodsTypes);
		List<AgentLevel> levels = agentLevelHandler.findAllOrderByLevel();
		model.addAttribute("levels",levels);
		return "/sub/audit_edit";
	}

	@ModelAttribute("parameter")
	public SearchParameter<PmallGoods> preprocess(
			@RequestParam("id") Optional<Integer> id) {
		SearchParameter<PmallGoods> parameter = new SearchParameter<PmallGoods>();
		PmallGoods goods;
		if (id.isPresent()) {
			goods =  mallGoodsHandler.get(id.get());
		} else {
			goods = new PmallGoods();
		}
		parameter.setEntity(goods);
		return parameter;
	}

	@Autowired
	private SubGoodsHandler subGoodsHandler;

	@Autowired
	private MallGoodsHandler mallGoodsHandler;

	@Autowired
	private MallGoodsTypeHandler mallGoodsTypeHandler;

	@Autowired
	private AgentLevelHandler agentLevelHandler;

	private final Logger LOG = LoggerFactory.getLogger(getClass());

}
